﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.Collections;
using System.Windows.Forms;
using System.Drawing;

namespace CIRDiagnoseDevice.Test
{
    class CommunicationWithInterfaceUnit
    {
        //判断是否有指标需要测试
        public bool JudgeIsNeedTest(DataGridView dgv, int startIndex, int count)
        {
            int num = 0;
            for (int i = startIndex; i < startIndex + count; i++)
            {
                if (dgv[0, i].Value.ToString() == "True")
                {
                    num++;
                    break;
                }
            }
            if (num == 0)
                return false;
            else
                return true;
        }

        //通知接口单元复位GSM-R数据模块电源
        public void SendResetPower(SerialPort OSP5)
        {
            try
            {
                List<byte> listData = new List<byte>();
                listData.Add(0x00);
                listData.Add(0x0f);
                byte[] data = listData.ToArray();
                byte[] sendData = PacketInterData(data);
                OSP5.Write(sendData, 0, sendData.Length);
            }
            catch
            {

            }
        }

        //通知接口单元清除GSM-R数据模块电源复位
        public void SendClearResetPower(SerialPort OSP5)
        {
            try
            {
                List<byte> listData = new List<byte>();
                listData.Add(0x00);
                listData.Add(0x10);
                byte[] data = listData.ToArray();
                byte[] sendData = PacketInterData(data);
                OSP5.Write(sendData, 0, sendData.Length);
            }
            catch
            {

            }
        }

        //通知打开GPRS电源
        public void SendGPRSPowerOn(SerialPort OSP5)
        {
            if (!OSP5.IsOpen)
            {
                try
                {
                    OSP5.Open();
                }
                catch
                {
                }

            }
            try
            {
                List<byte> listData = new List<byte>();
                listData.Add(0x00);
                listData.Add(0x0B);
                byte[] data = listData.ToArray();
                byte[] sendData = PacketInterData(data);
                OSP5.Write(sendData, 0, sendData.Length);
            }
            catch
            {

            }
        }

        public void SendGPRSPowerOff(SerialPort OSP5)
        {
            if (!OSP5.IsOpen)
            {
                try
                {
                    OSP5.Open();
                }
                catch
                {
                }

            }
            try
            {
                List<byte> listData = new List<byte>();
                listData.Add(0x00);
                listData.Add(0x0C);
                byte[] data = listData.ToArray();
                byte[] sendData = PacketInterData(data);
                OSP5.Write(sendData, 0, sendData.Length);
            }
            catch
            {

            }
        }


        // 通知GPRS模块无效
        public void SendClearGPRSPower(SerialPort OSP5)
        {
            if (!OSP5.IsOpen)
            {
                try
                {
                    OSP5.Open();
                }
                catch
                {
                }

            }
            try
            {
                List<byte> listData = new List<byte>();
                listData.Add(0x00);
                listData.Add(0x0E);
                byte[] data = listData.ToArray();
                byte[] sendData = PacketInterData(data);
                OSP5.Write(sendData, 0, sendData.Length);
            }
            catch
            {

            }
        }
        
        // 通知GPRS有效
       public void SendOpenGPRSPower(SerialPort OSP5)
       {
           if (!OSP5.IsOpen)
           {
               try
               {
                   OSP5.Open();
               }
               catch
               {
               }

           }
           try
           {
               List<byte> listData = new List<byte>();
               listData.Add(0x00);
               listData.Add(0x0D);
               byte[] data = listData.ToArray();
               byte[] sendData = PacketInterData(data);
               OSP5.Write(sendData, 0, sendData.Length);
           }
           catch
           {

           }
       }

        //延时多少毫秒
        public  void Delay(int mSecond)
        {
            int last_start;

            last_start = Environment.TickCount;
            while ((Environment.TickCount - last_start) <= mSecond)
            {
                System.Windows.Forms.Application.DoEvents();
            }
        } 
        
        /// <summary>
        /// 通知接口单元摘机
        /// </summary>
        /// <param name="OSP5"></param>
        public void SendInterUnitCallOn(SerialPort OSP5)
        {
            if (!OSP5.IsOpen)
            {
                try
                {
                    OSP5.Open();
                }
                catch
                {
                }

            }
            try
            {
                List<byte> listData = new List<byte>();
                listData.Add(0x00);
                listData.Add(0x01);
                byte[] data = listData.ToArray();
                byte[] sendData = PacketInterData(data);
                OSP5.Write(sendData, 0, sendData.Length);
            }
            catch { }
        }

        // 通知电源单元打开110V电源
        public void SendOpen110Power(SerialPort OSP)
        {
            if (!OSP.IsOpen)
            {
                try
                {
                    OSP.Open();
                }
                catch
                {
                }

            }
            try
            {
                List<byte> listData = new List<byte>();
                listData.Add(0x00);
                listData.Add(0xf2);
                byte[] data = listData.ToArray();
                byte[] sendData = PacketInterData(data);
                OSP.Write(sendData, 0, sendData.Length);
            }
            catch
            {

            }

        }

        //通知电源单元关闭110V电源
        public void SendClose110Power(SerialPort OSP)
        {
            if (!OSP.IsOpen)
            {
                try
                {
                    OSP.Open();
                }
                catch
                {
                }

            }
            try
            {
                List<byte> listData = new List<byte>();
                listData.Add(0x00);
                listData.Add(0xf3);
                byte[] data = listData.ToArray();
                byte[] sendData = PacketInterData(data);
                OSP.Write(sendData, 0, sendData.Length);
            }
            catch
            { }
        }

        //通知电源单元打开24V电源
        public void SendOpen24Power(SerialPort OSP)
        {
            if (!OSP.IsOpen)
            {
                try
                {
                    OSP.Open();
                }
                catch
                {
                }

            }
            try
            {
                List<byte> listData = new List<byte>();
                listData.Add(0x00);
                listData.Add(0xf4);
                byte[] data = listData.ToArray();
                byte[] sendData = PacketInterData(data);
                OSP.Write(sendData, 0, sendData.Length);          
            }
            catch
            {

            }
        }

        //通知电源单元关闭24V电源
        public void SendClose24Power(SerialPort OSP)
        {
            if (!OSP.IsOpen)
            {
                try
                {
                    OSP.Open();
                }
                catch
                {
                }

            }
            try
            {
                List<byte> listData = new List<byte>();
                listData.Add(0x00);
                listData.Add(0xf5);
                byte[] data = listData.ToArray();
                byte[] sendData = PacketInterData(data);
                OSP.Write(sendData, 0, sendData.Length);
            }
            catch
            {

            }
        }

        //通知电源单元测试110V电源功率
        public void SendTest110Power(SerialPort OSP)
        {
            if (!OSP.IsOpen)
            {
                try
                {
                    OSP.Open();
                }
                catch
                {
                }

            }
            try
            {
                List<byte> listData = new List<byte>();
                listData.Add(0x00);
                listData.Add(0xf0);
                byte[] data = listData.ToArray();
                byte[] sendData = PacketInterData(data);
                OSP.Write(sendData, 0, sendData.Length);
            }
            catch
            {

            }
        }

        //通知电源单元测试24V电源功率
        public void SendTest24Power(SerialPort OSP)
        {
            if (!OSP.IsOpen)
            {
                try
                {
                    OSP.Open();
                }
                catch
                {
                }

            }
            try
            {
                List<byte> listData = new List<byte>();
                listData.Add(0x00);
                listData.Add(0xf1);
                byte[] data = listData.ToArray();
                byte[] sendData = PacketInterData(data);
                OSP.Write(sendData, 0, sendData.Length);
            }
            catch
            {

            }
        } 
        
        /// <summary>
        /// //通知接口单元挂机
        /// </summary>
        /// <param name="OSP5"></param>
        public void SendInterUnitCallOff(SerialPort OSP5)
        {
            if (!OSP5.IsOpen)
            {
                try
                {
                    OSP5.Open();
                }
                catch
                {
                }

            }
            try
            {
                List<byte> listData = new List<byte>();
                listData.Add(0x00);
                listData.Add(0x02);
                byte[] data = listData.ToArray();
                byte[] sendData = PacketInterData(data);
                OSP5.Write(sendData, 0, sendData.Length);
            }
            catch { }
        } 
        /// <summary>
        /// 通知接口单元打开PTT
        /// </summary>
        /// <param name="OSP5"></param>
        public void SendOpenPTT(SerialPort OSP5)
        {
            if (!OSP5.IsOpen)
            {
                try
                {
                    OSP5.Open();
                }
                catch
                {
                }

            }
            try
            {
                List<byte> listData = new List<byte>();
                listData.Add(0x00);
                listData.Add(0x03);
                byte[] data = listData.ToArray();
                byte[] sendData = PacketInterData(data);
                OSP5.Write(sendData, 0, sendData.Length);
            }
            catch { }
        }

        //通知接口单元释放PTT
        public void SendClosePTT( SerialPort OSP5)
        {
            if (!OSP5.IsOpen)
            {
                try
                {
                    OSP5.Open();
                }
                catch
                {
                }

            }
            try
            {
                List<byte> listData = new List<byte>();
                listData.Add(0x00);
                listData.Add(0x04);
                byte[] data = listData.ToArray();
                byte[] sendData = PacketInterData(data);
                OSP5.Write(sendData, 0, sendData.Length);
            }
            catch
            {


            }
        }
        
        // 通知音频切换到扬声器
        public void ChangeVoiceToSpeaker(SerialPort OSP5)
        {
            try
            {
                List<byte> listData = new List<byte>();
                listData.Add(0x00);
                listData.Add(0x08);
                byte[] data = listData.ToArray();
                byte[] sendData = PacketInterData(data);
                OSP5.Write(sendData, 0, sendData.Length);
            }
            catch
            {

            }
        }

        // 通知音频切换到耳机
        public void ChangeVoiceToEarCup(SerialPort OSP5)
        {
            try
            {
                List<byte> listData = new List<byte>();
                listData.Add(0x00);
                listData.Add(0x09);
                byte[] data = listData.ToArray();
                byte[] sendData = PacketInterData(data);
                OSP5.Write(sendData, 0, sendData.Length);
            }
            catch
            {

            }
        }

        /// <summary>
        /// 通知音频切换到LBJ
        /// </summary>
        /// <param name="OSP5"></param> 
        public void ChangeVoiceToLBJ(SerialPort OSP5)
        {
            try
            {
                List<byte> listData = new List<byte>();
                listData.Add(0x00);
                listData.Add(0x0a);
                byte[] data = listData.ToArray();
                byte[] sendData = PacketInterData(data);
                OSP5.Write(sendData, 0, sendData.Length);
            }
            catch
            {

            }
        }

        // 通知测试整机功率
        public void TestWholePower(SerialPort OSP5)
        {
            if (!OSP5.IsOpen)
            {
                try
                {
                    OSP5.Open();
                }
                catch
                {
                }

            }
            try
            {
                List<byte> listData = new List<byte>();
                listData.Add(0x00);
                listData.Add(0x0b);
                byte[] data = listData.ToArray();
                byte[] sendData = PacketInterData(data);
                OSP5.Write(sendData, 0, sendData.Length);
            }
            catch
            {

            }
        }
        //通知发送TAX数据
        public void SendTaxData(SerialPort OSP5)
        {
            if (!OSP5.IsOpen)
            {
                try
                {
                    OSP5.Open();
                }
                catch
                {
                }

            }
            try
            {
                List<byte> listData = new List<byte>();
                listData.Add(0x00);
                listData.Add(0x0D);
                byte[] data = listData.ToArray();
                byte[] sendData = PacketInterData(data);
                OSP5.Write(sendData, 0, sendData.Length);
            }
            catch
            {

            }
        }

        private byte[] PacketInterData(byte[] data)
        {
            List<byte> listData = new List<byte>();
            listData.Add(0x10);
            listData.Add(0x02);
            ushort crc = crc_16.CalculateCRC(data, 0, (ushort)data.Length);
            for (int i = 0; i < data.Length; i++)
            {
                listData.Add(data[i]);
            }
            listData.Add((byte)(crc >> 8));
            listData.Add((byte)crc);
            listData.Add(0x10);
            listData.Add(0x03);
            return listData.ToArray();
        }

        //打包数据
        public byte[] PacketData(byte[] data)
        {
            List<byte> listData = new List<byte>();

            int length = data.Length + 2;
            listData.Add((byte)(length >> 8));
            listData.Add((byte)length);
            for (int i = 0; i < data.Length; i++)
            {
                listData.Add(data[i]);
            }
            length = listData.Count;
            byte[] arr = new byte[length];
            listData.CopyTo(0, arr, 0, length);
            ushort crc_data = crc_16.CalculateCRC(arr, 0, (ushort)length);
            listData.Add((byte)(crc_data >> 8));
            listData.Add((byte)crc_data);
            byte[] data2 = listData.ToArray();
            listData.Clear();
            listData.Add(0x10);
            listData.Add(0x02);
            for (int i = 0; i < data2.Length; i++)
            {
                listData.Add(data2[i]);
                if (data2[i] == 0x10)
                    listData.Add(data2[i]);
            }
            listData.Add(0x10);
            listData.Add(0x03);
            return listData.ToArray();
        }

        //拆分数据包
        public byte[] DePacketData(byte[] data)
        {
            List<byte> returnData = new List<byte>();
            for (int i = 2; i < data.Length - 2; i++)
            {
                returnData.Add(data[i]);
                if (((byte)data[i]) == 0x10)
                    i++;
            }
            return returnData.ToArray();
        }

      
        /// <summary>
        ///   查找帧头和帧尾
        /// </summary>
        /// <param name="arr"> 要查找的数组</param>
        /// <param name="head1">要查找的第一个字节</param>
        /// <param name="head2"> 要查找的第二个字节</param>
        /// <param name="startIndex">查找的初始位置</param>
        /// <param name="findLength">查找的总长度</param>
        /// <param name="isFindHead"></param>
        /// <returns></returns>
        private int FindDataFrameHead(List<byte> arr, byte head1, byte head2, int startIndex, int findLength, bool isFindHead)
        {
            int index = -1;
            int length = 0;
            length = findLength > arr.Count ? arr.Count : findLength;
            for (int i = startIndex; i < length - 1; i++)
            {
                if (arr[i] == head1 && arr[i + 1] == head2)
                {
                    index = i;
                    break;
                }
            }
            return index;
        }

        public byte[] ExtractFrame(List<byte> ListBuffer)
        {
            int startIndex = 0;
            startIndex = FindDataFrameHead(ListBuffer, (byte)0x10, (byte)0x02, 0, ListBuffer.Count, true);
            if (startIndex < 0)
                return null;
            int endIndex = FindDataFrameHead(ListBuffer, (byte)0x10, (byte)0x03, startIndex, ListBuffer.Count, false);
            if (endIndex < 0)
                return null;
            int length = endIndex - startIndex + 2;
            byte[] readData = new byte[length];
            ListBuffer.CopyTo(startIndex, readData, 0, length);
            ListBuffer.RemoveRange(0, endIndex + 2);
            return readData;
        }

        //将测试结果填写到测试表中
        public void FillGridWithTestReData(DataGridView gridview, int rowIndex, string value)
        {
            if (gridview.InvokeRequired)
            {
                gridview.Invoke(new MethodInvoker(delegate
                {
                    gridview.Rows[rowIndex].Cells["测试值"].Value = value;
                }));
            }
            else
            {
                gridview.Rows[rowIndex].Cells["测试值"].Value = value;
            }
            lock (this)
            {
                gridview.Invoke(new MethodInvoker(delegate
                {
                    gridview.Refresh();
                }));
            }
        }

        //将测试判断结果填写到测试表中
        public void FillGridWithJudgeResult(DataGridView gridview, int rowIndex, string result, bool isGreen)
        {
            if (gridview.InvokeRequired)
            {
                gridview.Invoke(new MethodInvoker(delegate
                {
                    gridview.Rows[rowIndex].Cells["测试结果"].Value = result;
                    if (isGreen)
                        gridview.Rows[rowIndex].Cells["测试结果"].Style.ForeColor = Color.Green;
                    else
                        gridview.Rows[rowIndex].Cells["测试结果"].Style.ForeColor = Color.Red;
                }));
            }
            else
            {
                gridview.Rows[rowIndex].Cells["测试结果"].Value = result;
                if (isGreen)
                    gridview.Rows[rowIndex].Cells["测试结果"].Style.ForeColor = Color.Green;
                else
                    gridview.Rows[rowIndex].Cells["测试结果"].Style.ForeColor = Color.Red;
            }
        }

    }
}
